<script setup lang="ts">
import {statusOpts} from "@/utils/status";
import {deleteWemch, WemchForm, wemchList, WemchQuery} from "@/api/shop/wemch";
import defaultSettings from "@/settings";

defineOptions({
  name: "Wemch",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
const objFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<WemchQuery>({
  pageNum: 1,
  pageSize: 10,
  shop_title: ""
});

const lists = ref<WemchQuery[]>();
const statusOptions = statusOpts();
const editVisible = ref(false);
const currentId = ref<number | undefined>();
const formData = reactive<WemchForm>({
  status: 1,
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  wemchList(queryParams)
    .then(({data}) => {
      lists.value = data.items || null;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}


/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 打开表单弹窗 */
function openDialog(id?: number) {
  editVisible.value = true;
  if (id) {
    currentId.value = id;
  }
}

//关闭编辑
async function closeDialog(type?: any) {
  editVisible.value = false;
  currentId.value = undefined;

  if (type == 1) {
    handleQuery();
  }
}

/** 删除 */
function handleDelete(id: number) {
  if (!id) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    deleteWemch(id)
      .then(() => {
        ElMessage.success("删除成功");
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form class="pull-left" ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.shop_title" placeholder="店铺名称" clearable @keyup.enter="handleQuery"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search/>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh/>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-button v-hasPerm="['shop:wemch:add']" type="success" @click="openDialog()" class="pull-right">
        <i-ep-plus/>
        新增
      </el-button>
      <div class="clear"></div>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="lists">
        <el-table-column label="所属店铺" prop="shop_title"/>
        <el-table-column label="商户MchID" prop="mch_id"/>
        <el-table-column label="商户秘钥" prop="secret"/>
        <el-table-column label="证书Cert地址" prop="cert_path">
          <template #default="scope">
            <el-link :href="defaultSettings.imgDomain + scope.row.cert_path" type="primary" target="_blank" link
                     v-if="scope.row.cert_path">查看
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="证书key地址" prop="key_path">
          <template #default="scope">
            <el-link :href="defaultSettings.imgDomain + scope.row.key_path" type="primary" target="_blank" link
                     v-if="scope.row.key_path">查看
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="create_time"/>
        <el-table-column label="状态" align="center" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'info'">
              {{ statusOptions[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" align="center" width="140">
          <template #default="scope">
            <el-button v-hasPerm="['shop:wemch:edit']" type="primary" link size="small"
                       @click="openDialog(scope.row.id)">
              <i-ep-edit class="margin3-r"/>
              编辑
            </el-button>
            <el-button v-hasPerm="['shop:wemch:delete']" type="danger" link size="small"
                       @click="handleDelete(scope.row.id)">
              <i-ep-delete class="margin3-r"/>
              删除
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="handleQuery"/>
    </el-card>
    <editMch :visible="editVisible" :wemchId="currentId" @close="closeDialog"/>
  </div>
</template>

